home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_3 / a3_2.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  2.3 KB  |  75 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 3.2 (Upper-Triangularization Followed by Back Substitution).
  9. % Section    3.4,    Gaussian Elimination and Pivoting, Page 156
  10. echo on; clc; format long; clear
  11.  
  12. % This program solves linear systems AX = B.
  13.  
  14. % A is an n x n matrix, B is an n-dimensional vector.
  15.  
  16. % The augmented matrix [A,B] is formed with B in column n+1.
  17.  
  18. % Upper triangularization is followed by back substitution.
  19.  
  20. % Remark. uptrbk.m is used for Algorithm 3.2
  21.  
  22. pause % Press any key to continue.
  23.  
  24. clc;
  25. % Solve the system AX = B where the augmented matrix [A,B] is:
  26.  
  27. A = [1   5   4  -3;
  28.      4   8   4   0;
  29.      1   3   0  -2;
  30.      1   4   7   2];
  31.  
  32. B = [-4; 8; -4; 10];       % Enter B as a column vector.
  33.  
  34. X = uptrbk(A,B);
  35.  
  36. R = B - A*X;               % Verify that the residual is small.
  37.  
  38. pause % Press any key to continue.
  39.  
  40. Mx1 = 'Upper-triangularization followed by back substitution.';
  41. Mx2 = 'The matrix is A =';
  42. Mx3 = 'The vector B is displayed as B` =';
  43. Mx4 = 'The solution to AX = B is displayed as X` =';
  44. Mx5 = 'The residual R = B - A*X is displayed as R` = ';
  45. clc,echo off, diary output,...
  46. disp(''),disp(Mx1),disp(Mx2),disp(A),disp(Mx3),...
  47. disp(B'),disp(Mx4),disp(X'),disp(Mx5),disp(R'),diary off, echo on
  48.       % Press any key to perform Upper-triangularization 
  49. pause %followed by back substitution.
  50.  
  51. clc;
  52. % Solve the system AX = B where the augmented matrix [A,B] is:
  53.  
  54. A = [1.001   5.001   4.001  -3.001;
  55.      4.001   8.001   4.001   0.001;
  56.      1.001   3.001   0.001  -2.001;
  57.      1.001   4.001   7.001   2.001];
  58.  
  59. B = [-4.001; 8.001; -4.001; 10.001];
  60.  
  61. X = uptrbk(A,B);
  62.  
  63. R = B - A*X;               % Verify that the residual is small.
  64.  
  65. pause % Press any key to continue.
  66.  
  67. Mx1 = 'Upper-triangularization followed by back substitution.';
  68. Mx2 = 'The matrix is A =';
  69. Mx3 = 'The vector B is displayed as B` =';
  70. Mx4 = 'The solution to AX = B is displayed as X` =';
  71. Mx5 = 'The residual R = B - A*X is displayed as R` = ';
  72. clc,echo off, diary output,...
  73. disp(''),disp(Mx1),disp(Mx2),disp(A),disp(Mx3),...
  74. disp(B'),disp(Mx4),disp(X'),disp(Mx5),disp(R'),diary off, echo on
  75.